package com.bawei.day0129.base;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProviders;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class BaseActivity<VM extends BaseViewModel,VDB extends ViewDataBinding> extends AppCompatActivity {
    public VM viewModel;
    public VDB dataBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding= DataBindingUtil.setContentView(this,initLayout());
        initViewModel();
        initView();
    }

    private void initViewModel() {
        Class cls=null;
        final Type type = getClass().getGenericSuperclass();
        if(type instanceof ParameterizedType){
            cls=(Class)((ParameterizedType)type).getActualTypeArguments()[0];
        }else {
            cls=getClass();
        }
        viewModel=(VM) ViewModelProviders.of(this).get(cls);
    }

    protected abstract int initLayout();
    protected abstract void initView();
}